//
//  ELUserDefault.m
//  jobClient
//
//  Created by 一览ios on 16/4/13.
//  Copyright © 2016年 YL1001. All rights reserved.
//

#import "ELUserDefault.h"

@implementation ELUserDefault
NSString *const kUserLoginStatus = @"kUserLoginStatus";
NSString *const kHomeTimeUserDefault = @"kHomeTimeUserDefault";
NSString *const kShowTip = @"kShowTip";
NSString *const kGroupHeadisPlay = @"kGroupHeadisPlay";
NSString *const kShowNewHead = @"kShowNewHead";
NSString *const kCompanyAddress = @"kCompanyAddress";
NSString *const kRecommendContent = @"kRecommendContent";
NSString *const kTabViewType = @"kTabViewType";
NSString *const kBindPhone = @"kBindPhone";
NSString *const kAudioBackPlayDic = @"kAudioBackPlayDic";
NSString *const kMessageHistorySearch = @"kMessageHistorySearch";
NSString *const kCompanyMessageHistorySearch = @"kCompanyMessageHistorySearch";
NSString *const kMediaCacheDate = @"kMediaCacheDate";
NSString *const kAudioBottomPlayerShowStatus = @"kAudioBottomPlayerShowStatus";
NSString *const kPlayerCacheTime = @"kPlayerCacheTime";
NSString *const kGroupAlertTimeDate = @"kGroupAlertTimeDate";
NSString *const kGroupMediaPlayHistoryList = @"kGroupMediaPlayHistoryList";
NSString *const kGroupMediaPlayHistoryLast = @"kGroupMediaPlayHistoryLast";
NSString *const kBindNewSystemEnterpriseInfo = @"kBindNewSystemEnterpriseInfo";
NSString *const kCompanyMessageDeleteListDic = @"kCompanyMessageDeleteListDic";

//@end
//@implementation ELUserDefault
+(void)setUserLoginStatus:(BOOL)status
{
    [[NSUserDefaults standardUserDefaults] setBool:status forKey:kUserLoginStatus];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+(BOOL)getUserLoginStatus
{
    return [[[NSUserDefaults standardUserDefaults] objectForKey:kUserLoginStatus] boolValue];
}

+(void)setHomeTimeUserDefault:(NSString *)time
{
    [[NSUserDefaults standardUserDefaults] setObject:time forKey:kHomeTimeUserDefault];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

//获取home时间戳
+(NSString *)getHomeTimeUserDefault
{
    return [[NSUserDefaults standardUserDefaults] objectForKey:kHomeTimeUserDefault];
}

+(void)setPerfectInformation:(BOOL)showTip
{
    [[NSUserDefaults standardUserDefaults] setBool:showTip forKey:kShowTip];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+(BOOL)getPerfectInfomation
{
    return [[NSUserDefaults standardUserDefaults] boolForKey:kShowTip];
}

+(void)setGroupHeadisPlay:(NSString *)time
{
    [[NSUserDefaults standardUserDefaults] setObject:time forKey:kGroupHeadisPlay];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+(NSString *)getGroupHeadisPlay
{
    return [[NSUserDefaults standardUserDefaults] objectForKey:kGroupHeadisPlay];
}

+(void)setShowNewHead:(NSString *)time
{
    [[NSUserDefaults standardUserDefaults] setObject:time forKey:kShowNewHead];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+(NSString *)getShowNewHead
{
    return [[NSUserDefaults standardUserDefaults] objectForKey:kShowNewHead];
}


+ (void)setCompanyAddress:(NSString *)address
{
    [[NSUserDefaults standardUserDefaults] setObject:address forKey:kCompanyAddress];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+ (NSString *)getCompanyAddress
{
    return [[NSUserDefaults standardUserDefaults] objectForKey:kCompanyAddress];
}

+ (void)setRecommendContent:(NSString *)content
{
    [[NSUserDefaults standardUserDefaults] setObject:content forKey:kRecommendContent];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+ (NSString *)getRecommendContent
{
    return [[NSUserDefaults standardUserDefaults] objectForKey:kRecommendContent];
}

+ (void)setBindPhoneStatus:(BOOL)status
{
    [[NSUserDefaults standardUserDefaults] setBool:status forKey:kBindPhone];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+ (BOOL)getBindPhoneStatus
{
    return [[NSUserDefaults standardUserDefaults] boolForKey:kBindPhone];
}

+(void)setkAudioBackPlayDic:(NSDictionary *)dic{
    if (dic) {
        [[NSUserDefaults standardUserDefaults] setObject:dic forKey:kAudioBackPlayDic];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
}

+(NSDictionary *)getkAudioBackPlayDic{
   return [[NSUserDefaults standardUserDefaults] objectForKey:kAudioBackPlayDic];
}

//消息搜索历史记录
+(void)setMessageSearchHistory:(NSMutableArray *)arr{
    [[NSUserDefaults standardUserDefaults] setObject:arr forKey:kMessageHistorySearch];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+(NSArray *)getMessageSearchHistory{
    return [[NSUserDefaults standardUserDefaults] objectForKey:kMessageHistorySearch];
}

+(void)clearMessageSearchHistory{
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:kMessageHistorySearch];
}

//企业端消息搜索历史记录
+(void)setCompanyMessageSearchHistory:(NSMutableArray *)arr{
    [[NSUserDefaults standardUserDefaults] setObject:arr forKey:kCompanyMessageHistorySearch];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+(NSArray *)getCompanyMessageSearchHistory{
    return [[NSUserDefaults standardUserDefaults] objectForKey:kCompanyMessageHistorySearch];
}

+(void)clearCompanyMessageSearchHistory{
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:kCompanyMessageHistorySearch];
}

//音频缓存记录上传时间
+(NSString *)getMediaCacheDate{
    return [[NSUserDefaults standardUserDefaults] objectForKey:kMediaCacheDate];
}

+(void)setMediaCacheDateWithStr:(NSString *)dateStr{
    [[NSUserDefaults standardUserDefaults] setObject:dateStr forKey:kMediaCacheDate];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

//音频显示底部播放器
+(BOOL)getBottomPlayerShowStatus{
    return [[NSUserDefaults standardUserDefaults] boolForKey:kAudioBottomPlayerShowStatus];
}

+(void)setBottomPlayerShowStatus:(BOOL)show{
    [[NSUserDefaults standardUserDefaults] setBool:show forKey:kAudioBottomPlayerShowStatus];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

//音频播放时间缓存
+(CGFloat)getPlayerCacheTime{
    return [[NSUserDefaults standardUserDefaults] floatForKey:kPlayerCacheTime];
}

+(void)setPlayerCacheTime:(CGFloat)time{
    [[NSUserDefaults standardUserDefaults] setFloat:time forKey:kPlayerCacheTime];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

//社群弹窗的时间
+(NSString *)getGroupAlertTimeDateWithId:(NSString *)groupId{
    return [[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithFormat:@"%@%@",kGroupAlertTimeDate,groupId]];
}

+(void)setGroupAlertTimeDate:(NSString *)time withGroupId:(NSString *)groupId{
    [[NSUserDefaults standardUserDefaults] setObject:time forKey:[NSString stringWithFormat:@"%@%@",kGroupAlertTimeDate,groupId]];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

//音频播放记录存取
+(void)saveGroupPlayHistoryWithModel:(ELPlayHistoryModel *)model{
    if (!model || !model.articleId || !model.articleTitle || !model.playTime || !model.audioTime){
        return;
    }
    NSString *key = [NSString stringWithFormat:@"%@%@",kGroupMediaPlayHistoryList,[Manager getUserInfo].userId_];
    NSDictionary *dic = [[NSUserDefaults standardUserDefaults] objectForKey:key];
    NSMutableDictionary *allDic;
    if (dic && dic.count > 0) {
        ELPlayHistoryModel *oldModel = [ELUserDefault getPlayHistoryModelWithArticleId:model.articleId];
        if (oldModel && [oldModel.playFinish isEqualToString:@"1"]){
            return;
        }   
        allDic = [NSMutableDictionary dictionaryWithDictionary:dic];
    }else{
        allDic = [[NSMutableDictionary alloc] init];
    }
    
    NSMutableDictionary *dataDic = [model mj_keyValues];
    if (dataDic) {
        [allDic setObject:dataDic forKey:model.articleId];
    }
    [[NSUserDefaults standardUserDefaults] setObject:allDic forKey:key];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    [ELUserDefault savePlayHistoryLastModel:model];
}

+(ELPlayHistoryModel *)getPlayHistoryModelWithArticleId:(NSString *)articleId{
    if (!articleId) {
        return nil;
    }
    NSString *key = [NSString stringWithFormat:@"%@%@",kGroupMediaPlayHistoryList,[Manager getUserInfo].userId_];
    NSDictionary *dic = [[NSUserDefaults standardUserDefaults] objectForKey:key];
    if (dic){
        NSMutableDictionary *modelDic = [dic objectForKey:articleId];
        if (modelDic) {
            ELPlayHistoryModel *model = [[ELPlayHistoryModel alloc] init];
            [model setValuesForKeysWithDictionary:modelDic];
            return model;
        }
    }
    return nil;
}

//音频最后一条记录存取
+(ELPlayHistoryModel *)getPlayHistoryModelLast{
    NSString *key = [NSString stringWithFormat:@"%@%@",kGroupMediaPlayHistoryLast,[Manager getUserInfo].userId_];
    NSDictionary *dic = [[NSUserDefaults standardUserDefaults] objectForKey:key];
    if (dic){
        ELPlayHistoryModel *model = [[ELPlayHistoryModel alloc] init];
        [model setValuesForKeysWithDictionary:dic];
        return model;
    }
    return nil;
}

+(void)savePlayHistoryLastModel:(ELPlayHistoryModel *)model{
    if (!model) {
        return;
    }
    NSString *key = [NSString stringWithFormat:@"%@%@",kGroupMediaPlayHistoryLast,[Manager getUserInfo].userId_];
    NSMutableDictionary *dataDic = [model mj_keyValues];
    if (dataDic) {
        [[NSUserDefaults standardUserDefaults] setObject:dataDic forKey:key];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
}

#pragma mark - 新版企业端绑定信息
+ (NSDictionary *)getBindNewSystemEnterpriseInfo {
    if (StringIsEmpty([Manager getUserInfo].userId_)) return nil;
    return [[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithFormat:@"%@_%@",kBindNewSystemEnterpriseInfo,[Manager getUserInfo].userId_]];
}

+ (void)setBindNewSystemEnterpriseInfo:(NSDictionary *)infoDic {
    if (DictIsEmpty(infoDic) || StringIsEmpty([Manager getUserInfo].userId_)) return;
    NSMutableDictionary *cmDic = [NSMutableDictionary dictionary];
    for (id key in infoDic.allKeys) {
        if (![[infoDic objectForKey:key] isKindOfClass:NSNull.class]) {
            NSLog(@"key----------->%@",[infoDic objectForKey:key]);
            [cmDic setObject:[infoDic objectForKey:key] forKey:key];
        }
    }
    NSLog(@"cmDic----------->%@",cmDic);
    [[NSUserDefaults standardUserDefaults] setValue:cmDic forKey:[NSString stringWithFormat:@"%@_%@",kBindNewSystemEnterpriseInfo,[Manager getUserInfo].userId_]];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+ (void)deleteBindNewSystemEnterpriseInfo {
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:[NSString stringWithFormat:@"%@_%@",kBindNewSystemEnterpriseInfo,[Manager getUserInfo].userId_]];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+(void)addCompanyDeleteListWithKey:(NSString *)key dateStr:(NSString *)dataStr{
    if (!key || !dataStr){
        return;
    }
    NSString *subKey = [NSString stringWithFormat:@"%@%@",kCompanyMessageDeleteListDic,[Manager getUserInfo].userId_];
    NSDictionary *subDic = [[NSUserDefaults standardUserDefaults] objectForKey:subKey];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    if (subDic) {
        dic = [NSMutableDictionary dictionaryWithDictionary:subDic];
    }
    [dic setObject:dataStr forKey:key];
    [[NSUserDefaults standardUserDefaults] setObject:dic forKey:subKey];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+(NSDictionary *)getCompanyDeleteList{
    NSString *subKey = [NSString stringWithFormat:@"%@%@",kCompanyMessageDeleteListDic,[Manager getUserInfo].userId_];
    NSDictionary *subDic = [[NSUserDefaults standardUserDefaults] objectForKey:subKey];
    return subDic;
}

@end
